package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.WashAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.WashOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.activity.ParkWashStatus;
import com.pengcheng.taskapplication.mvp.view.activity.ParkWashStatusV2;
import com.pengcheng.taskapplication.mvp.view.activity.WashConfrimOrderActivity;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;
import com.pengcheng.taskapplication.wxapi.WXPayEntryActivity;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;

public class MyWashListFragment extends BaseFragment implements IView,WashAdapter.OnStopLisener{

    private UserPresenterV2 userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;

    @BindView(R.id.empty_view)
    View empty_view;

    ArrayList<WashOrderModel> list =new ArrayList<>();
    private WashAdapter adapter;

    private int page=0;
    private int pagesize=10;
    private String uid;
    private String newtoken;
    private String tradno="";
    private String deviceid="";

    @Override
    protected int layoutResID() {
        return R.layout.fragment_wash_list_;
    }

    @Override
    protected void initData() {
        uid= SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(context,ConstantValue.SpType.newtoken);

        userPresenter = new UserPresenterV2(this);

        //adapter
        adapter = new WashAdapter(context,list);
        adapter.setOnStopLisner(this);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                WashOrderModel model=(WashOrderModel) adapterView.getAdapter().getItem(i);
                //已经支付，进行中 未开始
                if(model.getStatus()==1&&(model.getEndstatus()==0||model.getEndstatus()==3)) {
                    if(model.getEndstatus()==3){
                        //先去调用下开始接口/然后根据状态，跳转到轮询页面
                        tradno=model.getTradno();
                        deviceid=model.getDeviceid();
                        startDevice(newtoken,uid,model.getTradno());
                    }else {
                        Intent intent = new Intent(context, ParkWashStatusV2.class);
                        intent.putExtra("tradno", model.getTradno());
                        intent.putExtra("deviceid", model.getDeviceid());
                        startActivity(intent);
                    }
                }else {
                    Intent intent = new Intent(context, WashConfrimOrderActivity.class);
                    intent.putExtra("from", 1);
                    intent.putExtra("tradno", model.getTradno());
                    startActivity(intent);
                }
            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                queryWashOrderList(newtoken,uid,page,pagesize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                queryWashOrderList(newtoken,uid,page,pagesize);
            }
        });
        pullToRefreshListView.setEmptyView(empty_view);

        queryWashOrderList(newtoken,uid,page,pagesize);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.queryWashOrderList)){
            Gson gson= new Gson();
            Type type =new TypeToken<ArrayList<WashOrderModel>>(){}.getType();
            ArrayList<WashOrderModel> orderModels= gson.fromJson(gson.toJson(data),type);
            list.addAll(orderModels);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
        if(actionType.equals(ConstantValue.RequestKey.startDevice)){
            if(code.equals("0")){
                //开启成功话，进入到轮询页面进行轮询
                Intent intent =new Intent(context, ParkWashStatusV2.class);
                intent.putExtra("tradno",tradno);
                intent.putExtra("deviceid",deviceid);
                context.startActivity(intent);

            }else{
                //提示为什么开启失败
                ToastUtils.showShortToast(msg);
            }

        }
    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
    }

    public void queryWashOrderList(String token,String uid,int page,int pagesize){
        userPresenter.queryWashOrderList(token,uid,page,pagesize,ConstantValue.RequestKey.queryWashOrderList);
    }

    @Override
    public void onStop(WashOrderModel washOrderModel) {
        //停止机器
    }
    public void startDevice(String token,String uid,String tradno){
        userPresenter.startDevice(token,uid,tradno, ConstantValue.RequestKey.startDevice);
    }
    @Override
    public void onResume() {
        super.onResume();
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
    }
}
